<?php

namespace app\admin\controller;

use think\Controller;
use buildView\Button;
use buildView\Form;
use buildView\Grid;
use app\common\model\ShopGroup;
use app\common\model\ShopGroupUser;
/**
 * 用户分组管理
 * Class Group
 * @package app\admin\controller
 */
class Group extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $coupon_id = $this->request->get('coupon_id');
        $grid = new Grid(new ShopGroup);
        $grid->model()->withCount('user')->select();
		$grid->column('name','分组名称');
		$grid->column('remark','备注');
		$grid->column('user_count','分组人数');
		$grid->column('create_at','添加时间');
		$grid->filter(function ($filter){
            $filter->like('name','分组名称');
            $filter->dateBetween('create_at','添加时间');
        });
        $grid->hideExportButton();
        $grid->actions(function ($action,$data){
            $button = new Button('分组用户','warm');
            $button->href(url('record') ."?cid={$data['id']}");
            $action->prepend($button);
        	 $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $form = new Form(new ShopGroup);
		$form->text('name','名称')->required();
		$form->text('remark','备注');
        $user_arr = \app\common\model\User::select()->column('search_tip','id');
        $form->select('info','分组用户')->options($user_arr)->multiple()->required();
        $form->saved(function($post,$model){
            $group_id = $model->id;
            if ($post['info']){
                if ($post['id']){
                    ShopGroupUser::where('group_id',$post['id'])->setField('is_deleted',1);
                }
                $user = explode(',',$post['info']);
                $data = [];
                foreach ($user as $key => $val){
                    $data[] =[
                        'group_id' => $group_id,
                        'uid' => $val
                    ];
                }
                (new ShopGroupUser)->saveAll($data);
            }
            return $post;
        });
        return $form->view();
    }

    /**
     * 分组用户列表
     * @auth true
     * @menu true
     * @return string
     */
    public function record()
    {
        $cid = $this->request->get('cid');
        $grid = new Grid(new ShopGroupUser);
        $grid->model()->where('group_id', $cid)->select();
        $grid->user()->userInfo();
        $grid->user()->phone('手机号');
        $grid->filter(function ($filter){
            $filter->eq('user.phone','手机号');
        });
        $grid->hideAddButton();
        $grid->hideExportButton();
        $grid->actions(function ($action){
            $action->hideEdit();
            $action->hideDetail();
        });
        return $grid->view();
    }
}
